/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:itsm-gateway
 * Create Time: 2016年2月1日 下午2:22:37
 */
package com.lenovo.lmrp.server.itsmgateway.repository;

import java.util.Objects;

import javax.inject.Named;

import com.google.inject.Inject;
import com.lenovo.lmrp.api.model.entity.EngineerPO;
import com.lenovo.lmrp.api.model.entity.ProjectEngineerPO;
import com.lenovo.lmrp.api.model.entity.ProjectPO;
import com.lenovo.lmrp.server.itsmgateway.adapter.AItsmAdapterFactory;
import com.lenovo.lmrp.server.itsmgateway.domain.engineer.EngineerBO;
import com.lenovo.lmrp.server.itsmgateway.domain.engineer.IEngineerRepository;
import com.lenovo.sdc.framework.annotation.DomainRepository;
import com.lenovo.sdc.framework.dao.ADaoFactory;
import com.lenovo.sdc.framework.dao.ICommonDAO;
import com.lenovo.sdc.framework.dao.ICommonDAO.FinderEnum;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * 
 * @author wujin
 * @version v1.0
 */
@Named
@DomainRepository
public class EngineerRepository implements IEngineerRepository {
    
    private final AItsmAdapterFactory adapterFactory;
    
    @Inject
    public EngineerRepository(AItsmAdapterFactory adapterFactory) {
        this.adapterFactory = adapterFactory;
    }
    
    public EngineerBO createEngineerBO(String itsmCode, String loginName) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
            EngineerPO po = dao.get(EngineerPO.class, loginName);
            if (Objects.isNull(po)) {
                po = new EngineerPO();
                po.setLoginName(loginName);
            }
            
            return new EngineerBO(adapterFactory.getAdapter(itsmCode), itsmCode, po);
        }
        catch (Exception exp) {
            throw new TraceableException(ErrorCode.DB_RECORD_NOTFOUND, exp);
        }
    }
    
    @Override
    public void storeSyncProjectEngineer(String itsmCode, ProjectEngineerPO projectEngineerPO) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
            dao.beginTrans();

            ProjectEngineerPO oldPO = dao.get(ProjectEngineerPO.class, projectEngineerPO.getId());
            if (Objects.isNull(oldPO)) {
                dao.save(projectEngineerPO);
            }
            else {
                dao.evict(oldPO);
                dao.update(projectEngineerPO);
            }
            
            if (Objects.nonNull(projectEngineerPO.getProject())) {
                int count = dao.getRegisteredFinder(FinderEnum.HQL).countRecord("from ProjectPO where id=?",
                    new Object[] { projectEngineerPO.getProjectId() });
                if (count > 0)
                    dao.update(projectEngineerPO.getProject());
                else
                    dao.save(projectEngineerPO.getProject());
            }
            
            if (Objects.nonNull(projectEngineerPO.getEngineer())) {
                EngineerPO engineerPO = dao.get(EngineerPO.class, projectEngineerPO.getEngineerId());
                if (Objects.nonNull(engineerPO)){
                    //don't changed address when update project-engineer mapping
                    engineerPO.setPhone(projectEngineerPO.getEngineer().getPhone());
                    engineerPO.setSpId(projectEngineerPO.getEngineer().getSpId());
                    dao.update(engineerPO);
                }
                else
                    dao.save(projectEngineerPO.getEngineer());
            }

            dao.endTrans();
        }
        catch (Exception exp) {
            throw new TraceableException(ErrorCode.DB_TRANS_FAILED, exp);
        }
    }
    
    public void storeEngineer(String itsmCode, EngineerPO engineerPO) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
            dao.beginTrans();
            
            dao.saveOrUpdate(engineerPO);
            
            dao.endTrans();
        }
        catch (Exception exp) {
            throw new TraceableException(ErrorCode.DB_TRANS_FAILED, exp);
        }
    }
    
    public void storeSyncProject(String itsmCode, ProjectPO projectPO) {
        try (ICommonDAO dao = ADaoFactory.getDefault().createDAO(itsmCode)) {
            dao.beginTrans();

            ProjectPO oldPO = dao.get(ProjectPO.class, projectPO.getId());
            if (Objects.isNull(oldPO)) {
                dao.save(projectPO);
            }
            else {
                oldPO.setStatus(projectPO.getStatus());
                oldPO.setName(projectPO.getName());
                dao.update(oldPO);
            }
            
            if (Objects.nonNull(projectPO.getOthers())) {
                int count = dao.getRegisteredFinder(FinderEnum.HQL).countRecord("from ProjectExtensePO where id=?",
                    new Object[] { projectPO.getId() });
                if (count > 0)
                    dao.update(projectPO.getOthers());
                else
                    dao.save(projectPO.getOthers());
            }

            dao.endTrans();
        }
        catch (Exception exp) {
            throw new TraceableException(ErrorCode.DB_TRANS_FAILED, exp);
        }
    }
}
